from xml.etree.cElementTree import ElementTree as et

class Parser:
    def __init__(self):
        parser = et()
        parser.parse("result_nocontrib.xml")

        it = parser.getiterator("duplication")
        self.results = {
            'dup': 0,
            'dup_lines': 0,
            'dup_lines_sum': 0,
            'dup_tokens': 0,
            'dup_tokens_sum': 0,
            'dup_in_same_file': 0,
            'dup_in_different_file': 0,
            'fileset': set()}
        while True:
            try:
                elem = it.next()
                self.results['dup'] += 1
                self.results['dup_lines'] += int(elem.attrib['lines'])
                self.results['dup_tokens'] += int(elem.attrib['tokens'])
                
                files = elem.findall("file")
                fileset = set()
                for file in files:
                    fileset.add(file.attrib['path'])
                    self.results['fileset'].add(file.attrib['path'])
                if len(fileset) > 1:
                    self.results['dup_in_different_file'] += 1
                if len(fileset) < len(files):
                    self.results['dup_in_same_file'] += 1
                
                self.results['dup_lines_sum'] += int(elem.attrib['lines']) * len(files)
                self.results['dup_tokens_sum'] += int(elem.attrib['tokens']) * len(files)
                 
                    
                
            except StopIteration:
                break
        
    def getResults(self):
        return self.results

if __name__ == "__main__":
    CPP_PLUS_HEADER_FILES = 113 + 119
    CPP_PLUS_HEADER_LINES = 35787 + 9225
    
    parser = Parser()
    results = parser.getResults()
    print "Number of duplications: %d"%(results['dup'])
    print "-------------------------------"
    print "Duplicated lines (unique): %d"%(results['dup_lines'])
    print "Duplicated lines (sum): %d"%(results['dup_lines_sum'])
    print "Ratio of lines part of duplications: %f"%(float(results['dup_lines_sum']) / CPP_PLUS_HEADER_LINES)
    
    print "-------------------------------"
    print "Duplicated tokens (unique): %d"%(results['dup_tokens'])
    print "Duplicated tokens (sum): %d"%(results['dup_tokens_sum'])
    
    print "-------------------------------"
    print "Code duplicated in the same file: %d"%(results['dup_in_same_file'])
    print "Code duplicated in different files: %d"%(results['dup_in_different_file'])
    
    print "-------------------------------"
    print "Average duplication length (lines): %d"%(results['dup_lines'] / results['dup'])
    print "Average duplication length (tokens): %d"%(results['dup_tokens'] / results['dup'])
    
    print "-------------------------------"
    print "Number of files containing duplications: %d"%(len(results['fileset']))
    print "Ratio of files containing duplications: %f"%(float(len(results['fileset'])) / CPP_PLUS_HEADER_FILES)